package cn.gok.controller;
import cn.gok.service.ServiceService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/api/server")
public class ServiceController {
    @Autowired
    private ServiceService service;

    //列表
    @RequestMapping("/show")
    @ResponseBody
    public JSONObject show(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("searchKey",json.getString("searchKey"));
//        map.put("start",json.getInteger("start"));
//        map.put("pagesize",json.getInteger("pagesize"));
        JSONObject result =new JSONObject();
        result.put("data",service.show(map));
        result.put("total",service.show(map).size());
        return result;
    }
    //增加
    @RequestMapping("/add")
    @ResponseBody
    public JSONObject add(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("name",json.getString("name"));
        map.put("ip",json.getString("ip"));
        map.put("state",json.getString("state"));
        map.put("mark",json.getString("mark"));
        map.put("user",json.getString("user"));
        JSONObject result =new JSONObject();
        result.put("code",service.add(map));
        result.put("msg","添加成功");
        return result;
    }
    //修改
    @RequestMapping("/re")
    @ResponseBody
    public JSONObject re(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("name",json.getString("name"));
        map.put("ip",json.getString("ip"));
        map.put("state",json.getString("state"));
        map.put("mark",json.getString("mark"));
        map.put("id",json.getInteger("id"));
        map.put("user",json.getString("user"));
        JSONObject result =new JSONObject();
        if (service.updata(map)!=0){
            result.put("code",service.updata(map));
            result.put("msg","修改成功");
        }else {
            result.put("msg","修改失败");
        }
        return result;
    }
    //删除
    @RequestMapping("/del")
    @ResponseBody
    public JSONObject del(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("id",json.getInteger("id"));
        map.put("user",json.getString("user"));
        JSONObject result =new JSONObject();
        if (service.del(map)!=0){
            result.put("code",1);
            result.put("msg","删除成功");
        }else {
            result.put("code",0);
            result.put("msg","删除失败");
        }
        return result;
    }
    //详情
    @RequestMapping("/details")
    @ResponseBody
    public JSONObject details(@RequestBody JSONObject json){
        Map<String,Object> map=new HashMap<>();
        map.put("name",json.getString("name"));
        JSONObject result =new JSONObject();
        result.put("data",service.details(map));
        return result;
    }


}
